﻿Public Class BuscFactura

    Dim classController As New Class_RC_Controller.rc_Facturas()
    Dim clientes As New Class_RC_Controller.rc_Clientes

    Private _conProductoSaldo As Boolean = False

    Public Property conProductoSaldo() As Boolean
        Get
            Return _conProductoSaldo
        End Get
        Set(value As Boolean)
            _conProductoSaldo = value
        End Set
    End Property

    Private _conSaldo As Boolean = True

    Public Property conSaldo() As Boolean
        Get
            Return _conSaldo
        End Get
        Set(value As Boolean)
            _conSaldo = value
        End Set
    End Property

    Private _esVenta As Boolean = True

    Public Property esVenta() As Boolean
        Get
            Return _esVenta
        End Get
        Set(value As Boolean)
            _esVenta = value
        End Set
    End Property

    Private _codCliente As Integer = 0

    Public Property codCliente() As Integer
        Get
            Return _codCliente
        End Get
        Set(value As Integer)
            _codCliente = value
        End Set
    End Property

    Private _codProveedor As Integer = 0

    Public Property codProveedor() As Integer
        Get
            Return _codProveedor
        End Get
        Set(value As Integer)
            _codProveedor = value
        End Set
    End Property

    Private _codFactura As Integer = 0

    Public Property codFactura() As Integer
        Get
            Return _codFactura
        End Get
        Set(value As Integer)
            _codFactura = value
        End Set
    End Property

    Private _nroFactura As String = ""

    Public Property nroFactura() As String
        Get
            Return _nroFactura
        End Get
        Set(value As String)
            _nroFactura = value
            txtNro.Text = value
        End Set
    End Property

    Private _fechaFactura As DateTime = Date.Now

    Public Property fechaFactura() As DateTime
        Get
            Return _fechaFactura
        End Get
        Set(value As DateTime)
            _fechaFactura = value
        End Set
    End Property

    Private _totalFactura As Decimal = 0

    Public Property totalFactura() As Decimal
        Get
            Return _totalFactura
        End Get
        Set(value As Decimal)
            _totalFactura = value
        End Set
    End Property

    Private _saldoFactura As Decimal = 0

    Public Property saldoFactura() As Decimal
        Get
            Return _saldoFactura
        End Get
        Set(value As Decimal)
            _saldoFactura = value
        End Set
    End Property

    Private Sub CargarVentas()

        Dim tipoVenta As String = ""
        If cbxTipo.Checked Then
            tipoVenta = cboTipo.Text
        End If

        Dim estadoVenta As String = ""
        If cbxEstado.Checked Then
            estadoVenta = cboEstado.Text
        End If

        Dim vendedorVenta As String = ""
        If cbxVendedor.Checked Then
            vendedorVenta = cboVendedor.Text
        End If

        Dim dt As New DataTable
        dt = classController.get_Ventas_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, tipoVenta, "",
        estadoVenta, txtNro.Text, "", vendedorVenta, codCliente, conSaldo, conProductoSaldo)
        dgVentas.DataSource = dt
        dgVentas.Select()

        If Not classController.MessageError Is Nothing Then
            MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

    End Sub

    Private Sub CargarCompras()

        Dim tipoVenta As String = ""
        If cbxTipo.Checked Then
            tipoVenta = cboTipo.Text
        End If

        Dim estadoVenta As String = ""
        If cbxEstado.Checked Then
            estadoVenta = cboEstado.Text
        End If

        Dim vendedorVenta As String = ""
        If cbxVendedor.Checked Then
            vendedorVenta = cboVendedor.Text
        End If

        Dim dt As New DataTable
        If esVenta Then
            dt = classController.get_Ventas_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, tipoVenta, "",
        estadoVenta, txtNro.Text, "", vendedorVenta, codCliente, conSaldo, conProductoSaldo)
            dgVentas.DataSource = dt
            dgVentas.Select()
        Else
            dt = classController.get_Compras_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, tipoVenta, "",
        estadoVenta, txtNro.Text, "", codProveedor, conSaldo)
            dgCompras.DataSource = dt
            dgCompras.Select()
        End If
        

        If Not classController.MessageError Is Nothing Then
            MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

    End Sub

    Private Sub txtBuscar_Click(sender As System.Object, e As System.EventArgs) Handles txtBuscar.Click
        CargarDatos()
    End Sub

    Private Sub BuscFactura_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        fechaInicio.Value = Date.Now.AddDays(-30)

        dgVentas.AutoGenerateColumns = False
        dgCompras.AutoGenerateColumns = False

        If esVenta Then

            CargarVentas()
            cbxVendedor.Visible = True
            cboVendedor.Visible = True
            dgVentas.Visible = True
            dgCompras.Visible = False

        Else

            cbxVendedor.Visible = False
            cboVendedor.Visible = False
            dgVentas.Visible = False
            dgCompras.Visible = True

            CargarCompras()
            dgCompras.Columns(5).Visible = False

        End If

    End Sub

    Private Sub CargarDatos()
        If esVenta Then
            CargarVentas()
        Else
            CargarCompras()
        End If
    End Sub


    Private Sub txtNro_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtNro.KeyDown
        If e.KeyCode = Keys.Enter Then

            CargarDatos()

        End If
    End Sub

    Private Sub cbxEstado_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cbxEstado.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarDatos()
        End If
    End Sub

    Private Sub cboEstado_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cboEstado.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarDatos()
        End If
    End Sub

    Private Sub cbxVendedor_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cbxVendedor.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarDatos()
        End If
    End Sub

    Private Sub cboVendedor_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cboVendedor.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarDatos()
        End If
    End Sub

    Private Sub cbxTipo_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cbxTipo.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarDatos()
        End If
    End Sub

    Private Sub cboTipo_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cboTipo.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarDatos()
        End If
    End Sub

    Private Sub fechaInicio_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles fechaInicio.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarDatos()
        End If
    End Sub

    Private Sub fechaFin_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles fechaFin.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarDatos()
        End If
    End Sub

    Private Sub dgVentas_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles dgVentas.KeyDown, dgCompras.KeyDown
        If e.KeyCode = Keys.Enter Then
            Seleccionar()
        End If
    End Sub

    Private Sub dgVentas_CellContentDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgVentas.CellDoubleClick, dgCompras.CellDoubleClick
        Seleccionar()
    End Sub

    Private Sub Seleccionar()
        Try
            If esVenta Then

                If dgVentas.SelectedRows.Count > 0 Then

                    codFactura = dgVentas.SelectedRows(0).Cells(0).Value
                    nroFactura = dgVentas.SelectedRows(0).Cells(2).Value
                    fechaFactura = dgVentas.SelectedRows(0).Cells(1).Value
                    totalFactura = dgVentas.SelectedRows(0).Cells(5).Value
                    saldoFactura = dgVentas.SelectedRows(0).Cells(6).Value

                    Me.Close()

                End If

            Else
                If dgCompras.SelectedRows.Count > 0 Then

                    codFactura = dgCompras.SelectedRows(0).Cells(0).Value
                    nroFactura = dgCompras.SelectedRows(0).Cells(2).Value
                    fechaFactura = dgCompras.SelectedRows(0).Cells(1).Value
                    totalFactura = dgCompras.SelectedRows(0).Cells(5).Value
                    saldoFactura = dgCompras.SelectedRows(0).Cells(6).Value

                    Me.Close()

                End If

            End If
            

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try
    End Sub

End Class